//
//  TabBarViewController.swift
//  DouyinDemo
//
//  Created by MAC on 2022/3/7.
//

import UIKit

class TabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //首页
        let feedRootVC=FeedRootViewController()
        addChildViewController(childViewController: feedRootVC, title: "首页")
        
        //朋友
        let friendRootVC=FriendRootViewController()
        addChildViewController(childViewController: friendRootVC, title: "朋友")
        
        //相机
        let cameraRootVC=CameraViewController()
        let addImage=UIImage(named: "icon_home_add")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        addChildViewController(childViewController: cameraRootVC, title: nil,image: addImage,selectImage: addImage)
        
        //消息
        let messageRootVC=MessageRootViewController()
        addChildViewController(childViewController: messageRootVC, title: "消息")
        
        //我
        let profileRootVC=ProfileRootViewController()
        addChildViewController(childViewController: profileRootVC, title: "我")
        
    }
    
    //MARK:Private
    private func addChildViewController(childViewController:UIViewController,title:String?,image:UIImage?=nil,selectImage:UIImage?=nil){
        let navigationController=UINavigationController(rootViewController: childViewController)//导航控制器
        navigationController.setNavigationBarHidden(true, animated: false)//隐藏导航栏
        
        let tabBarItem = navigationController.tabBarItem
        tabBarItem?.title=title
        tabBarItem?.image=image
        tabBarItem?.selectedImage=selectImage
        
        if let _ = title{
            //标题
            tabBarItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.red,.font:UIFont.systemFont(ofSize: 15)], for: UIControl.State.normal)
            //.font全写是NSAttributedString.Key.font
            tabBarItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.white,.font:UIFont.systemFont(ofSize: 15)], for: UIControl.State.selected)
            tabBarItem?.titlePositionAdjustment=UIOffset(horizontal: 0, vertical: -15)
            
        }else{
            //图片
            tabBarItem?.imageInsets=UIEdgeInsets(top: 5.5, left: 0, bottom: -5.5, right: 0)
        }
        addChild(navigationController)
    }
    
    
}
